package 设计二构造版本2;

import java.util.Random;

/**
 * 算式测试类，用于测试加法和减法算式类的功能
 */
public class FormulaTest {
    
    private static int testCount = 0;
    private static int passedCount = 0;
    
    /**
     * 主方法，运行所有测试
     */
    public static void main(String[] args) {
        System.out.println("========== 开始测试算式类 ==========\n");
        
        testAddFormula();
        testSubtractFormula();
        testAddFormulaEquality();
        testSubtractFormulaEquality();
        testBoundaryConditions();
        
        System.out.println("\n========== 测试完成 ==========");
        System.out.printf("总测试数: %d, 通过测试数: %d\n", testCount, passedCount);
        
        if (testCount == passedCount) {
            System.out.println("所有测试通过！");
        } else {
            System.out.println("有测试失败！");
        }
    }
    
    /**
     * 测试加法算式类
     */
    private static void testAddFormula() {
        System.out.println("1. 测试加法算式类：");
        AddFormula addFormula = new AddFormula();
        Random random = new Random(123); // 使用固定种子以便测试结果可重现
        
        for (int i = 0; i < 5; i++) {
            addFormula.generate(random);
            boolean valid = addFormula.getResult() == addFormula.getLeftOperand() + addFormula.getRightOperand();
            boolean sumValid = addFormula.getResult() <= 100;
            
            System.out.printf("   测试%d: %s, 结果正确: %s, 和≤100: %s\n", 
                            i+1, addFormula.toString(), valid, sumValid);
            
            testCount++;
            if (valid && sumValid) {
                passedCount++;
            }
        }
        System.out.println();
    }
    
    /**
     * 测试减法算式类
     */
    private static void testSubtractFormula() {
        System.out.println("2. 测试减法算式类：");
        SubtractFormula subtractFormula = new SubtractFormula();
        Random random = new Random(456); // 使用固定种子以便测试结果可重现
        
        for (int i = 0; i < 5; i++) {
            subtractFormula.generate(random);
            boolean valid = subtractFormula.getResult() == subtractFormula.getLeftOperand() - subtractFormula.getRightOperand();
            boolean operandValid = subtractFormula.getLeftOperand() >= subtractFormula.getRightOperand();
            
            System.out.printf("   测试%d: %s, 结果正确: %s, 被减数≥减数: %s\n", 
                            i+1, subtractFormula.toString(), valid, operandValid);
            
            testCount++;
            if (valid && operandValid) {
                passedCount++;
            }
        }
        System.out.println();
    }
    
    /**
     * 测试加法算式的相等性（验证交换律）
     */
    private static void testAddFormulaEquality() {
        System.out.println("3. 测试加法算式相等性（交换律）：");
        
        // 测试1: 创建两个相同的加法算式
        AddFormula formula1 = new AddFormula();
        formula1.setLeftOperand(5);
        formula1.setRightOperand(3);
        formula1.calculate();
        
        AddFormula formula2 = new AddFormula();
        formula2.setLeftOperand(5);
        formula2.setRightOperand(3);
        formula2.calculate();
        
        boolean test1 = formula1.equals(formula2);
        boolean hashCodeEqual1 = formula1.hashCode() == formula2.hashCode();
        
        System.out.printf("   测试1: 相同算式相等性: %s, 哈希码相等: %s\n", test1, hashCodeEqual1);
        
        // 测试2: 创建满足交换律的加法算式
        AddFormula formula3 = new AddFormula();
        formula3.setLeftOperand(3);
        formula3.setRightOperand(5);
        formula3.calculate();
        
        boolean test2 = formula1.equals(formula3);
        boolean hashCodeEqual2 = formula1.hashCode() == formula3.hashCode();
        
        System.out.printf("   测试2: 交换操作数相等性: %s, 哈希码相等: %s\n", test2, hashCodeEqual2);
        
        testCount += 2;
        if (test1 && hashCodeEqual1) {
            passedCount++;
        }
        if (test2 && hashCodeEqual2) {
            passedCount++;
        }
        System.out.println();
    }
    
    /**
     * 测试减法算式的相等性（验证不满足交换律）
     */
    private static void testSubtractFormulaEquality() {
        System.out.println("4. 测试减法算式相等性（不满足交换律）：");
        
        // 测试1: 创建两个相同的减法算式
        SubtractFormula formula1 = new SubtractFormula();
        formula1.setLeftOperand(8);
        formula1.setRightOperand(3);
        formula1.calculate();
        
        SubtractFormula formula2 = new SubtractFormula();
        formula2.setLeftOperand(8);
        formula2.setRightOperand(3);
        formula2.calculate();
        
        boolean test1 = formula1.equals(formula2);
        boolean hashCodeEqual1 = formula1.hashCode() == formula2.hashCode();
        
        System.out.printf("   测试1: 相同算式相等性: %s, 哈希码相等: %s\n", test1, hashCodeEqual1);
        
        // 测试2: 创建交换操作数的减法算式（不应该相等）
        SubtractFormula formula3 = new SubtractFormula();
        formula3.setLeftOperand(3);
        formula3.setRightOperand(8);
        formula3.calculate();
        
        boolean test2 = !formula1.equals(formula3);
        boolean hashCodeEqual2 = formula1.hashCode() != formula3.hashCode();
        
        System.out.printf("   测试2: 交换操作数不相等性: %s, 哈希码不相等: %s\n", test2, hashCodeEqual2);
        
        testCount += 2;
        if (test1 && hashCodeEqual1) {
            passedCount++;
        }
        if (test2 && hashCodeEqual2) {
            passedCount++;
        }
        System.out.println();
    }
    
    /**
     * 测试边界条件
     */
    private static void testBoundaryConditions() {
        System.out.println("5. 测试边界条件：");
        
        // 测试加法边界：和为100
        AddFormula addFormula = new AddFormula();
        addFormula.setLeftOperand(99);
        addFormula.setRightOperand(1);
        addFormula.calculate();
        boolean addBoundary = addFormula.getResult() == 100;
        System.out.printf("   加法边界测试: %s + %s = %s, 正确: %s\n", 
                        addFormula.getLeftOperand(), addFormula.getRightOperand(), 
                        addFormula.getResult(), addBoundary);
        
        // 测试减法边界：被减数等于减数
        SubtractFormula subtractFormula = new SubtractFormula();
        subtractFormula.setLeftOperand(50);
        subtractFormula.setRightOperand(50);
        subtractFormula.calculate();
        boolean subtractBoundary = subtractFormula.getResult() == 0;
        System.out.printf("   减法边界测试: %s - %s = %s, 正确: %s\n", 
                        subtractFormula.getLeftOperand(), subtractFormula.getRightOperand(), 
                        subtractFormula.getResult(), subtractBoundary);
        
        testCount += 2;
        if (addBoundary) {
            passedCount++;
        }
        if (subtractBoundary) {
            passedCount++;
        }
        System.out.println();
    }
}